package com.compomics.proteocloud.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.compomics.proteocloud.util.AWSUtils;
import com.compomics.util.gui.JLabelAndComponentPanel;

public class CredentialsDialog extends JDialog{
	
    private JTextField txtKeyPair;
	private JTextField txtGroup;
	private JTextField txtAccessKey;
	private JPasswordField txtSecretKey;
	private JButton btnOK;
	private JButton btnCancel;
	private ProteoCloudUI parent;
    
    /**
     * This constructor will initialize the component and construct the GUI.
     *
     * @param parent CloudGUI, which is the parent component for this dialog.
     */
    public CredentialsDialog(ProteoCloudUI parent) {
        super(parent, "Credentials", true);
        this.parent = parent;       
        this.constructScreen();
        this.pack();
        this.setResizable(false);
        PropertiesUI.centerInScreen(this);
        this.addWindowListener(new WindowAdapter() {    
            public void windowClosing(WindowEvent e) {
                close();
            }
        });
    }
    
	/**
     * This method initializes all components.
     */
    private void constructScreen() {
        // Next on the list are the DB components.
        JPanel jpanDB = new JPanel(new BorderLayout());
        jpanDB.setBorder(BorderFactory.createTitledBorder("AWS Settings"));

        txtKeyPair = new JTextField(35);
        txtGroup = new JTextField(35);
        txtAccessKey = new JTextField(35);
        txtSecretKey = new JPasswordField(35);
        this.getDefaultValues();       
        
        final JLabelAndComponentPanel jpanLT = new JLabelAndComponentPanel(new JLabel[]{new JLabel("Key Pair"), new JLabel("Security Group"), new JLabel("Access Key"), new JLabel("Secret Key")}, new JTextField[]{txtKeyPair, txtGroup, txtAccessKey, txtSecretKey});

        // Placing the DB components and their respective labels.
        jpanDB.add(jpanLT, BorderLayout.CENTER);

        // The buttonpanel.
        JPanel jpanButtons = this.getButtonPanel();

        // The main panel.
        JPanel jpanMain = new JPanel();
        jpanMain.setLayout(new BoxLayout(jpanMain, BoxLayout.Y_AXIS));
        jpanMain.add(jpanDB);
        jpanMain.add(Box.createVerticalStrut(10));
        jpanMain.add(jpanButtons);
        this.getContentPane().add(jpanMain, BorderLayout.CENTER);
    }
    
    /**
     * Retrieves the default values... change this for the beta!
     */
    private void getDefaultValues(){
    	// Get the properties object     
    	Properties props = AWSUtils.getProperties();
    	txtKeyPair.setText(props.getProperty(AWSUtils.DEFAULT_KEYPAIR));
        txtGroup.setText(props.getProperty(AWSUtils.DEFAULT_SECURITY_GROUP));
        txtAccessKey.setText(props.getProperty(AWSUtils.AWS_ACCESS_KEY_PROPERTY_NAME));
        txtSecretKey.setText(props.getProperty(AWSUtils.AWS_SECRET_KEY_PROPERTY_NAME));
    }
    
    
    /**
     * This method constructs the buttonpanel.
     *
     * @return JPanel  with the buttons.
     */
    private JPanel getButtonPanel() {
        JPanel buttons = new JPanel();        

        btnOK = new JButton("OK");
        btnOK.setMnemonic(KeyEvent.VK_O);
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                okPressed();
            }
        });

        btnCancel = new JButton("Cancel");
        btnCancel.setMnemonic(KeyEvent.VK_C);
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cancelPressed();
            }
        });

        buttons.add(btnOK);
        buttons.add(btnCancel);
        return buttons;
    }

    /**
     * This method is called when the 'Cancel' button is pressed.
     */
    private void cancelPressed() {
        this.close();
    }
    
    /**
     * This method closes the window without any other action.
     */
    private void close() {
        this.setVisible(false);
        this.dispose();
    }

    /**
     * This method is called when the 'OK' button is pressed.
     */
    private void okPressed() {
       
        String keypair = txtKeyPair.getText().trim();
        if (keypair == null || keypair.equals("")) {
            this.fillOutComponentWarning("KeyPair");
            txtKeyPair.requestFocus();
            return;
        }
        String group = txtGroup.getText().trim();
        if (group == null || group.equals("")) {
            this.fillOutComponentWarning("SecurityGroup");
            txtGroup.requestFocus();
            return;
        }
   
        parent.setCredentials(keypair, group, txtAccessKey.getText().trim(), String.valueOf(txtSecretKey.getPassword()));
        this.setVisible(false);
        this.dispose();
    }

    
    /**
     * This method displays an error message about the necessity of filling out the specified component first.
     *
     * @param aComponent String with the description of the data for the component that needs to be filled out.
     */
    private void fillOutComponentWarning(String aComponent) {
        String lMessage = "You need to fill out the " + aComponent + " first!";
        JOptionPane.showMessageDialog(this, lMessage, aComponent + " not filled out!", JOptionPane.ERROR_MESSAGE);
    }
}
